package com.bocai.grainshop.coupon.service.impl;

import com.bocai.grainshop.coupon.entity.SeckillSkuRelationEntity;
import com.bocai.grainshop.coupon.service.SeckillSkuRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.common.utils.PageUtils;
import com.bocai.common.utils.Query;

import com.bocai.grainshop.coupon.dao.SeckillSessionDao;
import com.bocai.grainshop.coupon.entity.SeckillSessionEntity;
import com.bocai.grainshop.coupon.service.SeckillSessionService;

@Slf4j
@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService skuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getSeckileeForTreeDay() {
        //计算三天的时间区间
        List<String> timeForTree = getDateTimeForTree(LocalDate.now());
        log.info("时间区间:---->"+timeForTree.get(0)+"------"+timeForTree.get(1));
        //查询数据库
        List<SeckillSessionEntity> start_time = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", timeForTree.get(0),timeForTree.get(1)));
        //查询当前场次需要上架的商品
        if(start_time.size() > 0 && start_time != null){
            List<SeckillSessionEntity> collect = start_time.stream().map(o->{
                List<SeckillSkuRelationEntity> list = skuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", o.getId()));
                if(list.size() > 0 && list != null){
                    o.setSeckillSkuRelation(list);
                }
                return o;
            }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    //获取三天时间区间
    private List<String> getDateTimeForTree(LocalDate time){
        LocalTime max = LocalTime.MAX;
        LocalTime min = LocalTime.MIN;
        LocalDateTime end = LocalDateTime.of(time.plusDays(2), max);
        String endF = end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime start = LocalDateTime.of(time, min);
        String startF = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        List<String> localDateTimes = new ArrayList<>();
        localDateTimes.add(startF);
        localDateTimes.add(endF);
        return localDateTimes;
    }

}